package com.sc.nft.consume;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.dto.UserFirstBuyDTO;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.enums.*;
import com.sc.nft.enums.task.TaskOrderTypeEnum;
import com.sc.nft.enums.task.TaskTypeEnum;
import com.sc.nft.service.TaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;

/**
 * 首次消费任务
 */
@RocketMQMessageListener(topic = MessageTopic.USER_FIRST_BUY+"${spring.profiles.active}",consumerGroup =  MessageTopic.USER_FIRST_BUY+"${spring.profiles.active}")
@Component
@Slf4j
@RequiredArgsConstructor
public class UserFirstBuyConsumer implements RocketMQListener<UserFirstBuyDTO> {

    private final TaskService taskService;
    private final UserInfoDao userInfoDao;

    @Override
    public void onMessage(UserFirstBuyDTO userFirstBuyDTO) {
        log.info("首次消费任务：{}",userFirstBuyDTO);
        UserInfo userInfo = userInfoDao.getById(userFirstBuyDTO.getUserId());
        taskService.finishTask(userInfo, TaskOrderTypeEnum.FIRST_BUY,TaskTypeEnum.USER_BOW_PIN,userFirstBuyDTO.getLinkId(),null, BigDecimal.ZERO);
        UserInfo inviteUser = userInfoDao.getById(userInfo.getRecommendUserId());
        if(ObjectUtil.isNull(inviteUser)){
            return;
        }
        taskService.finishTask(inviteUser,  TaskOrderTypeEnum.FIRST_BUY,TaskTypeEnum.BOW_PIN_USER_BOW_PINY,userFirstBuyDTO.getLinkId(),null, BigDecimal.ZERO);
    }
}
